<?php

use Core\AdminController;
use Models\Evaluate;
use Models\Product;

class EvaluateController extends AdminController
{
	public function index()
	{
		$query = Evaluate::order_by('id desc');
		if ($this->input->get('star')) {
			$query->where('star', $this->input->get('star'));
		}
		if ($this->input->get('item_id')) {
			$query->where('item_id', $this->input->get('item_id'));
		}
		if ($this->input->get('product_id')) {
			$query->where('product_id', $this->input->get('product_id'));
		}

		$pages = $query->with(['product', 'item'])
			->paginate(20);

		$products = Product::select('id,title,en_title')->get();

		return $this->load->view('admin/evaluate/index', [
			'pages' => $pages,
			'products' => $products
		]);
	}

	public function detail($id)
	{
		$evaluate = (new Evaluate)->find($id);
		if (!$evaluate) {
			return $this->response([], 404);
		}

		return $this->response($evaluate->toArray());
	}

	public function set_status($id)
	{
		$evaluate = (new Evaluate)->find($id);
		if (!$evaluate) {
			return $this->response([], 404);
		}

		$evaluate->update(null, [
			'status' => $this->input->post('status')
		]);

		return $this->response();
	}

	public function delete($id)
	{
		$evaluate = (new Evaluate)->find($id);
		if (!$evaluate) {
			return $this->response([], 404);
		}

		$evaluate->delete();

		return $this->response();
	}
}